#include <stdio.h>
#include <string.h>
#include <math.h>

struct node
{
	char psw[20];
	};
struct node a[100];


int hf(char str[],int opt)
{int i;int key = 0;
  switch(opt)
{  case 1:
         key=strlen(str);
         break;
  case 2:for(i=0;i<strlen(str);i++)
	 {key +=str[i];
         break;
  case 3:for(i=0;i<strlen(str);i++)
	 key +=(str[i]-'a'+1);
	 break;
} }
  
return key%100;
}

void test(int opt)
{
  char uname[50],pass[50];
  printf("Enter username : ");
  scanf("%s",uname);
  int loc =hf(uname,opt);
  if(a[loc].psw ==""){ printf("Username doesn't exist!\n");return;}
  int tries = 0;
  while(tries < 3){
    printf("Enter Password : ");
    scanf("%s",pass);
    if(!(strcmp(pass,a[loc].psw)))
	{ printf("Login Authenticated! Welcome"); tries = -1; break;}
    else printf("Authentication Error! Please Try again!\n");
    tries+=1;
  }
  if(tries == 3) printf("Invalid Login Attempt... Please verify username/password!\n");
}

void main()
{int i=0,j=0,k=0,ch,opt;
char uname[50], passw[50]; 
printf("which hash function do you want to use(1/2/3)");
scanf("%d",&opt);
FILE *fin;
fin=fopen("/home/pranjal/Desktop/pass.txt","r");
int temporary = fscanf(fin,"%s",uname);

{a[j].psw[0]="";}
  while(temporary != EOF)
   {fscanf(fin,"%s",passw);
	i=hf(uname,opt);
	for(k=0;k<20;k++)
  	{a[i].psw[k]=passw[k];}
	temporary = fscanf(fin,"%s",uname);
   }
printf("1. Test the table (for user/pass match)\n3. Exit\n? ");
    scanf("%d",&ch);
    switch(ch){
      case 1: test(opt); break;
      case 2: exit(0);
    }}
